package com.liu.structure.queue;

import java.util.Stack;

/**
 * 剑指 Offer 09. 用两个栈实现队列
 */
public class Offer09 {
    /**
     * 用两个栈实现一个队列。
     * 队列的声明如下，请实现它的两个函数 appendTail 和 deleteHead ，
     * 分别完成在队列尾部插入整数和在队列头部删除整数的功能。(若队列中没有元素，deleteHead 操作返回 -1 )
     */
    public static void main(String[] args) {
        Offer09 queue = new Offer09();
        for (int i = 0; i < 10; i++) {
            queue.appendTail(i + "");
        }
        for (int i = 0; i < 10; i++) {
            System.out.println(queue.deleteHead());
        }
    }

    private Stack stack_in;
    private Stack stack_out;

    public Offer09() {
        this.stack_in = new Stack();
        this.stack_out = new Stack();
    }

    public void appendTail(String tailData) {
        stack_in.push(tailData);
    }


    public String deleteHead() {
        String headData = "";
        if (stack_out.isEmpty()) {
            int stackSize= stack_in.size();
            for (int i = 0; i < stackSize; i++) {
                stack_out.push(stack_in.pop());
            }
        }
        headData = (String) stack_out.pop();
        return headData;
    }
}
